/*
 * @Author: EltonLi lijiaming@uavi-tech.com
 * @Date: 2023-09-06 09:51:22
 * @LastEditors: EltonLi lijiaming@uavi-tech.com
 * @LastEditTime: 2023-09-11 16:37:49
 * @FilePath: \zino-fc-v4\ZINO\sensor\QMI8658\qmi8658.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE!
 */
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-06     UAVI02       the first version
 */
#ifndef __QMI8658_H__
#define __QMI8658_H__

#include "applications\ZINO_FC_V4.0\sensor\sensordata.h"


#define QMI8658_DEFAULT_ID 0X05

typedef enum {
        BIT7 = 1 << 7,
        BIT6 = 1 << 6,
        BIT5 = 1 << 5,
        BIT4 = 1 << 4,
        BIT3 = 1 << 3,
        BIT2 = 1 << 2,
        BIT1 = 1 << 1,
        BIT0 = 1 << 0,
    }BITS_E;

typedef enum QMIM8658_RegisterMapOverview
{
    QMI8658_REG_WHO_AM_I = 0,     // Device Identifier, 0x05 - to identify the device is a QST sensor
    QMI8658_REG_REVISON_ID,       // Device Revision ID
    QMI8658_REG_CTRL1,            // SPI Interface and Sensor Enable, Control power states, configure SPI communications
    QMI8658_REG_CTRL2,            // Accelerometer: Output Data Rate, Full Scale, Self Test
    QMI8658_REG_CTRL3,            // Gyroscope: Output Data Rate, Full Scale, Self Test
    QMI8658_REG_CTRL4,            // Magnetometer Settings: Output Data Rate, and Device Selection
    QMI8658_REG_CTRL5,            // Low pass filter setting.
    QMI8658_REG_CTRL6,            // AttitudeEngine™ Settings: Output Data Rate, Motion on Demand
    QMI8658_REG_CTRL7,            // Enable Sensors. Individually Enable/Disable the AttitudeEngine, Accelerometer, Gyroscope and Magnetometer Using sEN, aEN, gEN, and mENbits, respectively.
    QMI8658_REG_CTRL8,            // Reserved: Not Used
    QMI8658_REG_CTRL9,            // Host Commands
    QMI8658_REG_CAL1_L,           // Calibration Register. CAL1_L – lower 8 bits. CAL1_H – upper 8 bits.
    QMI8658_REG_CAL1_H,           //
    QMI8658_REG_CAL2_L,           // Calibration Register. CAL2_L – lower 8 bits. CAL2_H – upper 8 bits.
    QMI8658_REG_CAL2_H,           //
    QMI8658_REG_CAL3_L,           // Calibration Register. CAL2_L – lower 8 bits. CAL2_H – upper 8 bits.
    QMI8658_REG_CAL3_H,           //
    QMI8658_REG_CAL4_L,           // Calibration Register. CAL4_L – lower 8 bits. CAL4_H – upper 8 bits.
    QMI8658_REG_CAL4_H,           //
    QMI8658_REG_FIFO_WTM_TH,      // FIFO watermark level, in ODRs
    QMI8658_REG_FIFO_CTRL,        // FIFO Setup
    QMI8658_REG_FIFO_SMPL,        // FIFO sample count LSBs
    QMI8658_REG_FIFO_STATUS,      // FIFO Status
    QMI8658_REG_FIFO_DATA,        // FIFO Data
    QMI8658_REG_I2CM_STATUS = 44, // I2C Master Status.
    QMI8658_REG_STATUSINT,        // Sensor Data Availability with the Locking mechanism.
    QMI8658_REG_STATUS0,          // Output Data Over Run and Data Availability.
    QMI8658_REG_STATUS1,          // Miscellaneous Status: Wake on Motion, CmdDone (CTRL9 protocol bit).
    QMI8658_REG_TIMESTAMP_LOW,    // Sample Time Stamp. TIMESTAMP_LOW – lower 8 bits.
    QMI8658_REG_TIMESTAMP_MID,    // Sample Time Stamp. TIMESTAMP_MID – middle 8 bits.
    QMI8658_REG_TIMESTAMP_HIGH,   // Sample Time Stamp. TIMESTAMP_HIGH – upper 8 bits.
    QMI8658_REG_TEMP_L,           // Temperature Output Data
    QMI8658_REG_TEMP_H,           //
    QMI8658_REG_AX_L,             // X-axis Acceleration
    QMI8658_REG_AX_H,             //
    QMI8658_REG_AY_L,             // Y-axis Acceleration
    QMI8658_REG_AY_H,             //
    QMI8658_REG_AZ_L,             // Z-axis Acceleration
    QMI8658_REG_AZ_H,             //
    QMI8658_REG_GX_L,             // Y-axis Angular Rate
    QMI8658_REG_GX_H,             //
    QMI8658_REG_GY_L,             // Y-axis Angular Rate
    QMI8658_REG_GY_H,             //
    QMI8658_REG_GZ_L,             // Y-axis Angular Rate
    QMI8658_REG_GZ_H,             //
    QMI8658_REG_MX_L,             // X-axis Magnetic Field
    QMI8658_REG_MX_H,             //
    QMI8658_REG_MY_L,             // Y-axis Magnetic Field
    QMI8658_REG_MY_H,             //
    QMI8658_REG_MZ_L,             // Z-axis Magnetic Field
    QMI8658_REG_MZ_H,             //
    QMI8658_REG_dQW_L = 73,       // Quaternion Increment dQW
    QMI8658_REG_dQW_H,            //
    QMI8658_REG_dQX_L,            // Quaternion Increment dQX
    QMI8658_REG_dQX_H,            //
    QMI8658_REG_dQY_L,            // Quaternion Increment dQY
    QMI8658_REG_dQY_H,            //
    QMI8658_REG_dQZ_L,            // Quaternion Increment dQZ
    QMI8658_REG_dQZ_H,            //
    QMI8658_REG_dVX_L,            // Velocity Increment along X-axis
    QMI8658_REG_dVX_H,            //
    QMI8658_REG_dVY_L,            // Velocity Increment along Y-axis
    QMI8658_REG_dVY_H,            //
    QMI8658_REG_dVZ_L,            // Velocity Increment along Z-axis
    QMI8658_REG_dVZ_H,            //
    QMI8658_REG_AE_REG_1,         // AttitudeEngine Register 1
    QMI8658_REG_AE_REG_2,         // AttitudeEngine Register 2
    QMI8658_REG_RESET = 96,       // Soft Reset Register
} QMI8658_REGISTER_MAP_E;         //

//start bit 6, 3bits
typedef enum
{
    QMI8658_CTRL2_ACC_RANGE_2G = 0,
    QMI8658_CTRL2_ACC_RANGE_4G = 1,
    QMI8658_CTRL2_ACC_RANGE_8G = 2,
    QMI8658_CTRL2_ACC_RANGE_16G = 3,
} QMI8658_CTRL2_ACC_RANGE_E;

//start bit 3, 4bits
typedef enum
{
    QMI8658_ACC_ODR_NORMAL_8000Hz = 0,
    QMI8658_ACC_ODR_NORMAL_4000Hz,
    QMI8658_ACC_ODR_NORMAL_2000Hz,
    QMI8658_ACC_ODR_NORMAL_1000Hz,
    QMI8658_ACC_ODR_NORMAL_500Hz,
    QMI8658_ACC_ODR_NORMAL_250Hz,
    QMI8658_ACC_ODR_NORMAL_125Hz,
    QMI8658_ACC_ODR_NORMAL_62_5Hz,
    QMI8658_ACC_ODR_NORMAL_31_25Hz,
    // QMI8658_ACC_ODR_LOWPOWER_128Hz = 12,
    // QMI8658_ACC_ODR_LOWPOWER_21Hz,
    // QMI8658_ACC_ODR_LOWPOWER_11Hz,
    // QMI8658_ACC_ODR_LOWPOWER_3Hz,
} QMI8658_CTRL2_ACC_ODR_E;

//start bit 6, 4bits
typedef enum
{
    QMI8658_GYRO_RANGE_16dps = 0,
    QMI8658_GYRO_RANGE_32dps,
    QMI8658_GYRO_RANGE_64dps,
    QMI8658_GYRO_RANGE_128dps,
    QMI8658_GYRO_RANGE_256dps,
    QMI8658_GYRO_RANGE_512dps,
    QMI8658_GYRO_RANGE_1024dps,
    QMI8658_GYRO_RANGE_2048dps,
} QMI8658_CTRL3_GYRO_RANGE_E;

//start bit 3, 4bits
typedef enum
{
    QMI8658_GYRO_ODR_NORMAL_8000Hz = 0,
    QMI8658_GYRO_ODR_NORMAL_4000Hz,
    QMI8658_GYRO_ODR_NORMAL_2000Hz,
    QMI8658_GYRO_ODR_NORMAL_1000Hz,
    QMI8658_GYRO_ODR_NORMAL_500Hz,
    QMI8658_GYRO_ODR_NORMAL_250Hz,
    QMI8658_GYRO_ODR_NORMAL_125Hz,
    QMI8658_GYRO_ODR_NORMAL_62_5Hz,
    QMI8658_GYRO_ODR_NORMAL_31_25Hz,
} QMI8658_CTRL3_GYRO_ODR_E;

//start bit 6,2bits
typedef enum
{
    QMI8658_CTRL5_aLFP_MODE_0 = 0, // 2.62% of ODR
    QMI8658_CTRL5_aLFP_MODE_1, // 3.59% of ODR
    QMI8658_CTRL5_aLFP_MODE_2, // 5.32% of ODR
    QMI8658_CTRL5_aLFP_MODE_3, // 14.0% of ODR
    //start bit 4,1bit
    // QMI8658_CTRL5_aLPF_EN = 1,

} QMI8658_CTRL5_ACC_GYRO_LPF_E;
//start bit 2,2bits
typedef enum
{
    QMI8658_CTRL5_gLFP_MODE_0 = 0, // 2.62% of ODR
    QMI8658_CTRL5_gLFP_MODE_1, // 3.59% of ODR
    QMI8658_CTRL5_gLFP_MODE_2, // 5.32% of ODR
    QMI8658_CTRL5_gLFP_MODE_3, // 14.0% of ODR
    //start bit 0,1bit
    // QMI8658_CTRL5_aLPF_EN = 1,

} QMI8658_CTRL5_GYRO_LPF_E;


typedef enum
    {
        QMI8658_CTRL7_DisableSyncSmpleMode = 0,
        QMI8658_CTRL7_EnableSyncSmpleMode = BIT7,
        QMI8658_CTRL7_HighSpeedInternalClock = BIT6,
        QMI8658_CTRL7_ClockBaseOnODR = 0,
        QMI8658_CTRL7_GyroFullMode = 0,//Gyroscope in Full Mode (Drive and Sense are enabled).
        QMI8658_CTRL7_GyroSnoozeMode = BIT4,//Gyroscope in Snooze Mode (only Drive enabled).This bit is effective only when gEN is set to 1.
        QMI8658_CTRL7_EnalbeAttitudeEngine = BIT3,//Enable AttitudeEngine orientation and velocity increment computation
        QMI8658_CTRL7_DisalbeAttitudeEngine = 0,// Disable AttitudeEngine orientation and velocity increment computation
        QMI8658_CTRL7_EnableMagnetmoeter = BIT2,//Enable Magnetometer
        QMI8658_CTRL7_DisableMagnetmoeter = 0,//Magnetometer placed in Standby or Power-down Mode.
        QMI8658_CTRL7_EnableGyro = BIT1,//Enable Gyroscope.
        QMI8658_CTRL7_DisableGyro = 0,//Gyroscope placed in Standby or Power-down Mode.
        QMI8658_CTRL7_EnableAcc = BIT0,//Enable Accelerometer.
        QMI8658_CTRL7_DiableAcc = 0,//Accelerometer placed in Standby or Power-down Mode.
    }QMI8658_CTRL7_E;

    typedef enum
    {
        QMI8658_ACC_ODR_SET,
        QMI8658_GYRO_ODR_SET,
        QMI8658_ACC_RANGE_SET,
        QMI8658_GYRO_RANGE_SET,
        QMI8658_ALPF_SET,
        QMI8658_GLPF_SET,
        QMI8658_MODE_SET,
    }QMI8658_CMD_E;

typedef struct
{
    struct rt_spi_device *spi_dev;
    range_config_t range;
    rt_uint8_t dev_id;
    Axis_3_int16_t acce_raw;
    Axis_3_int16_t gyro_raw;
    Axis_3_int32_t acce;
    Axis_3_int32_t gyro;
} qmi8658_device_t;

#endif